package com.secondary.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport
{

	public BaseAction()
	{
	}

	protected boolean isNull(Object NULL)
	{
		return NULL == null || NULL.toString().trim().length() == 0;
	}

	protected void setSessionInvalidate()
	{
		ServletActionContext.getRequest().getSession().invalidate();
	}

	protected boolean isExistSessionAttriubte(String key)
	{
		return ActionContext.getContext().getSession().get(key) != null;
	}

	protected void setApplicationAttribute(String key, Object obj)
	{
		ActionContext.getContext().getApplication().put(key, obj);
	}

	protected Object getApplicationAttribute(String key)
	{
		return ActionContext.getContext().getApplication().get(key);
	}

	protected void setSessionAttribute(String key, Object obj)
	{
		ActionContext.getContext().getSession().put(key, obj);
	}

	protected Object getSessionAttribute(String key)
	{
		return ActionContext.getContext().getSession().get(key);
	}

	protected Object removeSessionAttribute(String key)
	{
		return ActionContext.getContext().getSession().remove(key);
	}

	protected String getScheme()
	{
		return ServletActionContext.getRequest().getScheme();
	}

	protected String getRemoteAddr()
	{
		return ServletActionContext.getRequest().getRemoteAddr();
	}

	protected String getServerPort()
	{
		return (new StringBuilder()).append(ServletActionContext.getRequest().getServerPort()).toString();
	}

	protected String getContextPath()
	{
		return (new StringBuilder()).append(ServletActionContext.getRequest().getContextPath()).toString();
	}

	protected String getServletPath()
	{
		return ServletActionContext.getRequest().getServletPath();
	}

	protected String getQueryString()
	{
		return ServletActionContext.getRequest().getQueryString();
	}

	protected String getAccessMethod()
	{
		return ServletActionContext.getRequest().getMethod();
	}

	protected String getServletURL()
	{
		return (new StringBuilder(String.valueOf(getScheme()))).append("://").append(getRemoteAddr()).append(":").append(getServerPort()).append(getContextPath()).toString();
	}

	protected String getFullURL()
	{
		return (new StringBuilder(String.valueOf(getScheme()))).append("://").append(getRemoteAddr()).append(":").append(getServerPort()).append(getContextPath()).append(getServletPath()).append("?").append(getQueryString()).toString();
	}

	protected String getRequestParameter(String key)
	{
		return ServletActionContext.getRequest().getParameter(key);
	}

	protected Map getRequestParameterMap()
	{
		return ServletActionContext.getRequest().getParameterMap();
	}

	protected void setRequestAttribute(String key, Object obj)
	{
		ServletActionContext.getRequest().setAttribute(key, obj);
	}

	protected Object getRequestAttribute(String key)
	{
		return ServletActionContext.getRequest().getAttribute(key);
	}

	protected void setContextAttribute(String key, Object obj)
	{
		ActionContext.getContext().put(key, obj);
	}

	protected Object getContextAttribute(String key)
	{
		return ActionContext.getContext().get(key);
	}

	protected ActionContext getActionContext()
	{
		return ActionContext.getContext();
	}

	protected void saveActionError(String key)
	{
		super.addActionError(super.getText(key));
	}

	protected void saveActionMessage(String key)
	{
		super.addActionMessage(super.getText(key));
	}

	protected String getRequestPath()
	{
		return ServletActionContext.getRequest().getServletPath();
	}

	protected void copyBeanProperties(Object targer, Object source)
	{
		if (source != null)
			try
			{
				BeanUtils.copyProperties(targer, source);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
	}

	protected void renderText(String text)
	{
		try
		{
			ServletActionContext.getResponse().setContentType("text/plain;charset=UTF-8");
			ServletActionContext.getResponse().getWriter().write(text);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	protected void renderText(String text, String charset)
	{
		try
		{
			ServletActionContext.getResponse().setContentType((new StringBuilder("text/plain;charset=")).append(charset).toString());
			ServletActionContext.getResponse().getWriter().write(text);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	protected void renderHtml(String text)
	{
		try
		{
			ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.write(text);
			out.flush();
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	protected void renderHtml(String text, String charset)
	{
		try
		{
			ServletActionContext.getResponse().setContentType((new StringBuilder("text/html;charset=")).append(charset).toString());
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.write(text);
			out.flush();
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	protected void renderXML(String text)
	{
		try
		{
			ServletActionContext.getResponse().setContentType("text/xml;charset=UTF-8");
			ServletActionContext.getResponse().getWriter().write(text);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	protected void renderXML(String text, String charset)
	{
		try
		{
			ServletActionContext.getResponse().setContentType((new StringBuilder("text/xml;charset=")).append(charset).toString());
			ServletActionContext.getResponse().getWriter().write(text);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	protected ServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
}

      